package com.wanpinghui.supplier.widgets;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;
import com.wanpinghui.supplier.R;
import com.wanpinghui.supplier.message.adapter.P2PMsgAdapter;
import com.wanpinghui.wphlibrary.model.message.bean.MsgBean;
import com.wanpinghui.wphlibrary.model.order.bean.OrderGoodsFormatBean;
import com.wanpinghui.wphlibrary.model.order.bean.OrderGoodsItemBean;
import com.wanpinghui.wphlibrary.model.order.bean.OrderItemBean;
import com.yunshl.yunshllibrary.utils.NumberUtil;
import com.yunshl.yunshllibrary.utils.TextUtil;

/**
 * 功能说明：
 * Created by caitiangui
 * 创建时间： 2017/12/27.
 */

public class MsgLeftOrderView extends LinearLayout {

    private SpinView sv;
    private ImageView iv_error;
    private LinearLayout llmsg_product;
    private TextView textViewContent;
    private OrderItemBean orderItemBean;

    public MsgLeftOrderView(Context context) {
        super(context);
        init(context);
    }

    public MsgLeftOrderView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public MsgLeftOrderView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public MsgLeftOrderView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context);
    }

    private void init(Context context) {
        View mView = LayoutInflater.from(context).inflate(R.layout.item_msg_left_product, this);
        sv = (SpinView) mView.findViewById(R.id.sv);
        llmsg_product = (LinearLayout) mView.findViewById(R.id.llmsg_product);
        textViewContent = (TextView) mView.findViewById(R.id.tv_msg);
        iv_error = (ImageView) mView.findViewById(R.id.iv_error);

    }

    public void initData(MsgBean msgBean, int userId, final P2PMsgAdapter.OnMsgClickListener listener) {
        llmsg_product.removeAllViews();
        if (TextUtil.isNotEmpty(msgBean.getExt())) {
            try {
                orderItemBean = new Gson().fromJson(msgBean.getExt(), new TypeToken<OrderItemBean>() {
                }.getType());
            } catch (JsonSyntaxException e) {
                e.printStackTrace();
            }
        }
        if (orderItemBean != null) {
            if (orderItemBean.getBuyer_id() == userId) {
                textViewContent.setText("您好，我已代您采购以下产品！");
            } else {
                textViewContent.setText("您好，我要采购以下产品！");
            }
            if (orderItemBean.getItemList() != null && orderItemBean.getItemList().size() > 0) {
                for (OrderGoodsItemBean itemBean : orderItemBean.getItemList()) {
                    llmsg_product.addView(getItemView(itemBean));
                }
            }
            setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listener != null) {
                        listener.onOrderClick(orderItemBean);
                    }
                }
            });
            switch (msgBean.getSend_statu()) {
                case 0:
                    setError(false);
                    setSending(true);
                    break;
                case 1:
                    setError(false);
                    setSending(false);
                    break;
                case 2:
                    setError(true);
                    setSending(false);
                    break;
            }
        }
    }

    private View getItemView(OrderGoodsItemBean itemBean) {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.view_msg_left_product, null);
        TextView textViewName = (TextView) view.findViewById(R.id.tv_object_name);
        TextView tv_count = (TextView) view.findViewById(R.id.tv_count);
        ImageView imageView = (ImageView) view.findViewById(R.id.image);
        TextView textView = (TextView) view.findViewById(R.id.tv_more_format);
        if (itemBean.getOrderSpecItemList() != null && itemBean.getOrderSpecItemList().size() > 0) {
            textView.setVisibility(VISIBLE);
            StringBuilder formatStr = new StringBuilder();
            for (OrderGoodsFormatBean formatBean : itemBean.getOrderSpecItemList()) {
                formatStr.append(formatBean.getSpec_value()).append(" ").append(formatBean.is_consult() ? "议价" : ("￥" + NumberUtil.double2StringAdaptive(formatBean.getPrice())))
                        .append("X" + formatBean.getQuantity()).append(",");
            }
            formatStr.deleteCharAt(formatStr.length() - 1);
            textView.setText(formatStr.toString());
        } else {
            textView.setVisibility(GONE);
        }
        Glide.with(getContext()).load(itemBean.getMain_img()).placeholder(R.mipmap.loading_140x140).into(imageView);
        textViewName.setText(itemBean.getName());
        tv_count.setText("共 "+itemBean.getQuantity()+" 件     合计  ￥"+((itemBean.getAllTotal()==-1)?"议价": NumberUtil.double2StringAdaptive(itemBean.getAllTotal())));
        return view;
    }


    public void setSending(boolean isSendding) {
        if (isSendding)
            sv.setVisibility(View.VISIBLE);
        else
            sv.setVisibility(View.GONE);

    }

    public void setError(boolean isError) {
        if (isError)
            iv_error.setVisibility(View.VISIBLE);
        else
            iv_error.setVisibility(View.GONE);
    }

}
